package com.server.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.server.service.ICourMaterialsManageService;
import com.server.web.DataVector;
import com.server.web.RequestInput;

/**
 * 课程资料管理
 * @author wufeng
 * @date 创建时间：2016年7月13日 下午2:47:37 
 */
@Service("CourMaterialsManageService")
public class CourMaterialsManageService implements ICourMaterialsManageService{
	private static String tableName = "education.course_res";
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private CommonDataService commonDataService;
	
	
	@Override
	public DataVector<Map<String, Object>> getSourceList(RequestInput requestInput, Integer type) {
		String whereStr = "";
		if(type == 0){
			whereStr = null;
		}else{
			whereStr = "type = '"+type+"'";
		}
		String sortStr = " createTime desc";
		DataVector<Map<String, Object>> listData = commonDataService.getListData("education.v_courseRes_file", whereStr, sortStr, requestInput);
		return listData;
	}
	
	@Override
	public boolean update(String id, Map<String, Object> inputMap, String operater) {
		boolean isSuccess = commonDataService.update(tableName, operater, id, id, inputMap);
		return isSuccess;
	
	}
	
	@Override
	public Map<String, Object> getMap(String id) {
		String sql = "Select * from education.course_res where id='"+id+"'";
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = jdbcTemplate.queryForMap(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
	@Override
	public boolean delete(String id) {
		boolean isSuccess = true;
		String sql = "DELETE education.course_res,education.file FROM education.course_res"
				+ " LEFT JOIN education.file ON education.course_res.id=education.file.actionId WHERE education.file.actionType = 'course_res' and education.course_res.id='"+id+"'";
		String sql_1 = "delete from education.course_res where id = '"+id+"'";
		
		try {
			int update = jdbcTemplate.update(sql);
			if(update <= 0){
				try {
					int update_1 = jdbcTemplate.update(sql_1);
					if(update_1 <= 0){
						isSuccess = false;
					}
				} catch (Exception e) {
					isSuccess = false;
				}
				
			}
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}

	@Override
	public boolean isRight(String id, String user) {
		String sql = "select creater from "+tableName+" where id = '"+id+"'";
		String authorship = jdbcTemplate.queryForObject(sql, String.class); 
		if(!StringUtils.isEmpty(user)&&user.equals(authorship)){
			return true;
		}
		return false;
	}

	@Override
	public Map<String, Object> add(Map<String, Object> inputMap, String operator) {
		Map<String, Object> inputParamMap = new HashMap<String, Object>();
		inputParamMap.put("source_name", inputMap.get("source_name"));//VARCHAR
		inputParamMap.put("type", inputMap.get("type"));//VARCHAR
		inputParamMap.put("content", inputMap.get("content"));//VARCHAR
		inputParamMap.put("creater", operator);
		
		Map<String, Object> execute = new SimpleJdbcCall(jdbcTemplate).withCatalogName("education")
			.withProcedureName("P_COURSE_RES_ADD")
			.declareParameters(
						new SqlParameter("source_name",java.sql.Types.VARCHAR),
						new SqlParameter("type",java.sql.Types.INTEGER),
						new SqlParameter("content",java.sql.Types.VARCHAR),
						new SqlParameter("creater",java.sql.Types.VARCHAR),
						new SqlOutParameter("id",java.sql.Types.INTEGER)
			).execute(inputParamMap);
		return execute;	
	}

}
